import { defineStore } from 'pinia'
import { ApiService } from '../ts/ApiService'
import { BaseDataResult } from '../ts/types/BaseResult'
import { IpInfoData, IpInfoDataState, WeatherLive } from '../ts/types/Tools'
import { log } from '../ts/logger'

const store = defineStore('ipinfo', {
  state: (): IpInfoDataState => {
    return {
      ipinfo: {} as IpInfoData,
      weather: {} as WeatherLive,
    }
  },
  actions: {
    updateIpInfo(cb?: Function) {
      let instance = this
      ApiService.get('/tools/ipinfo', { ip: '' }, (data: BaseDataResult<IpInfoData>) => {
        if (data.success) {
          instance.ipinfo = data.data
          ApiService.get('/tools/weather/live', { city: data.data.citycode }, (data: BaseDataResult<WeatherLive>) => {
            if (data.success) {
              instance.weather = data.data
            }
          })
        }
        log.debug('ip信息', instance.ipinfo)
        if (cb) {
          cb(data.success)
        }
      })
    },
  },
})

export default store
